package com.zyw.mvc.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zyw.mvc.support.WrappedResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.util.Date;

/**
 * @author zhangyw
 * @date 16/12/16 17:02
 */
public abstract class BaseController {

    public WrappedResult wrappedSuccessResult(Object data) {
        return wrappedSuccessResult(data, null);
    }

    public WrappedResult wrappedSuccessResult(Object data, String msg) {
        WrappedResult wrappedResult = WrappedResult.handleSuccessWrappedResult(data);
        if (StringUtils.isNotEmpty(msg))
            wrappedResult.setMsg(msg);
        return wrappedResult;
    }

    public WrappedResult wrappedFaliedResult(String msg) {
        return WrappedResult.handleExceptionWrappedResult(-1, msg, null);
    }

    public WrappedResult wrappedFaliedResult(Integer code, String msg) {
        return WrappedResult.handleExceptionWrappedResult(code, msg, null);
    }

    /**
     * 初始化数据绑定 1. 将所有传递进来的String进行HTML编码，防止XSS攻击 2. 将字段中Date类型转换为String类型
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        // String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击
        binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                // setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
                setValue(text);
            }

            @Override
            public String getAsText() {
                Object value = getValue();
                return value != null ? value.toString() : "";
            }
        });
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                try {
                    setValue(DateUtils.parseDate(text));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            // @Override
            // public String getAsText() {
            // Object value = getValue();
            // return value != null ? DateUtils.formatDateTime((Date)value) : "";
            // }
        });

        // Double 类型转换
        binder.registerCustomEditor(Double.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(StringUtils.isEmpty(text) ? 0.0 : Double.valueOf(text));
            }

            @Override
            public String getAsText() {
                Object value = getValue();
                return value != null ? value.toString() : "0.0";
            }
        });

        // Integer 类型转换
        binder.registerCustomEditor(Integer.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(StringUtils.isEmpty(text) ? 0 : Integer.valueOf(text));
            }

            @Override
            public String getAsText() {
                Object value = getValue();
                return value != null ? value.toString() : "0";
            }
        });

        // JSONObject 类型转换
        binder.registerCustomEditor(JSONObject.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                setValue(StringUtils.isEmpty(text) ? 0 : JSON.parseObject(text));
            }
        });

        // JSONArray 类型转换
        binder.registerCustomEditor(JSONArray.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                setValue(StringUtils.isEmpty(text) ? 0 : JSON.parseArray(text));
            }
        });
    }
}
